<?php

namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Config;
use think\Session;
class Login extends Controller{

	//引入登录页面
	public function getLogin(){
		return $this->fetch("login/login");
	}
	//执行登录操作
	public function postLogindo(){
		$request = request();
		//获取验证码,对比验证码
		$fcode = $request->param('fcode');
		if(!captcha_check($fcode)){
			$this->error("验证码错误","/adminlogin/login");
		}else{
			//检测用户名
			$name = $request->param("username");
			$info = Db::table("admin_user")->where("loginname",$name)->select();
			if($info){
				//检测密码
				$pwd = $request->param("password");
				if($pwd == $info[0]['password']){
					//加入session
					Session::set('name',$info[0]['loginname']);
					Session::set('islogin',1);

					//获取用户权限信息 控制器cname 方法aname
					$list = Db::query("select n.cname,n.aname from a_role_node as rn,a_user_role as ur,a_node as n where n.id=rn.nid and rn.rid=ur.rid and ur.uid={$info[0]['id']}");
					// echo "<pre>";
					// var_dump($list);
					// die();

					//初始化权限

					
					foreach($list as $v){

						$nodelist[$v['cname']][]=$v['aname'];//将同一控制器的方法归到一起

						//判断 如果add方法 添加 adddo
						if($v['aname'] == "getadd"){
							$nodelist[$v['cname']][] = "postadddo";
						}
						//如果是update方法 添加 updatedo
						if($v['aname'] == 'getupdate'){
							$nodelist[$v['cname']][] = "postupdate";
						}
					}
					//将权限信息存入session
					Session::set("nodelist",$nodelist);
					// echo "<pre>";
					// var_dump($nodelist);die;
					
					$this->success("登录成功","/adminindex/index");

				}else{
					$this->error("密码错误","/adminlogin/login");
				}
			}else{
				$this->error("用户名不存在","/adminlogin/login");
			}
		}
	}

	//执行登出
	public function getLoginout(){
		Session::delete('name');
		Session::delete('islogin');
		$this->success("已登出","/adminlogin/login");
	}
}
